import { defineStore } from "pinia";
import {
    getUserDataAPi,
    addUserDataAPi,
    updateUserDataAPi,
    deleteUserDataAPi
} from '@/Api/User'
import EventBus from "@/utils/EventBus";

import { useCommon } from "./Common";

const CommonStore = useCommon()

export const useUser = defineStore("User", {
    state: () => {
        return {
            loading: false,
            searchForm: {
                userName: '',
                department: '',
                current: 1,
                pageNo: 1,
                pageSize: 10,
                total: 0,
                showSizeChanger: true,
                pageSizeOptions: ['10', '20', '30', '50'],
                showTotal: (total: number) => {
                    return `总数${total}条`
                },
                onChange(pageNo: number, pageSize: number) {
                    this.current = pageNo
                    this.pageNo = pageNo
                    this.pageSize = pageSize
                }
            },
            tableData: [],
            addForm: {} as any,
            editForm: {} as any
        }
    },
    actions: {
        async queryUser() {
            this.loading = true

            try {
                const data = await getUserDataAPi(this.searchForm) as any

                if (data) {
                    this.tableData = data[0]
                    this.searchForm.total = data[1][0].total
                    this.tableData.forEach((item: any, index) => {
                        item.index = (this.searchForm.pageNo! - 1) * this.searchForm.pageSize! + index + 1
                        if (item.illArea) {
                            item.department = `${item.department}-${item.illArea}`
                        }
                    })
                }
            } finally {
                this.loading = false
            }
        },
        async addUser() {
            if (this.addForm.departmentWard != undefined && this.addForm.departmentWard.length == 1) {
                this.addForm.department = this.addForm.departmentWard[0]
                this.addForm.illArea = ''
            } else if (this.addForm.departmentWard != undefined && this.addForm.departmentWard.length == 2) {
                this.addForm.department = this.addForm.departmentWard[0]
                this.addForm.illArea = this.addForm.departmentWard[1]
            } else {
                if (CommonStore.department != '检验科') {
                    this.addForm.department = CommonStore.department
                    this.addForm.illArea = CommonStore.illArea
                } else {
                    this.addForm.department = ''
                    this.addForm.illArea = ''
                }
            }



            const data = await addUserDataAPi(this.addForm) as any

            if (!data) {
                this.queryUser()
                EventBus.emit("AddSuccess")
            }
        },
        async editUser() {
            if (this.editForm.departmentWard != undefined && this.editForm.departmentWard.length == 1) {
                this.editForm.department = this.editForm.departmentWard[0]
                this.editForm.illArea = ''
            } else if (this.editForm.departmentWard != undefined && this.editForm.departmentWard.length == 2) {
                this.editForm.department = this.editForm.departmentWard[0]
                this.editForm.illArea = this.editForm.departmentWard[1]
            } else {
                if (CommonStore.department != '检验科') {
                    this.editForm.department = CommonStore.department
                    this.editForm.illArea = CommonStore.illArea
                } else {
                    this.editForm.department = ''
                    this.editForm.illArea = ''
                }
            }

            const data = await updateUserDataAPi(this.editForm) as any
            if (!data) {
                this.queryUser()
                EventBus.emit('UpdateSuccess')
            }
        },
        async deleteUser(id: number) {
            const data = await deleteUserDataAPi({ id }) as any
            if (!data) {
                this.queryUser()
                EventBus.emit('DeleteSuccess')
            }
        }
    }
});